package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Clazz;
import cn.wolfcode.crm.query.ClazzQueryObject;
import cn.wolfcode.crm.service.IClazzService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("clazz")
public class ClazzController {

    @Autowired
    private IClazzService clazzService;

    @RequestMapping("")
    @RequiresPermissions(value = {"班级首页","clazz:index"} ,logical = Logical.OR)
    public String index(){

        return "clazz/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"班级列表","clazz:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(ClazzQueryObject qo){
        PageResult result = clazzService.query(qo);
        return result;
    }

    //班级列表(不带分页)
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        List<Clazz> result = clazzService.list();
        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"班级保存","clazz:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Clazz entity){
        JSONResult result = new JSONResult();
        try{
            clazzService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"班级更新","clazz:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Clazz entity){
        JSONResult result = new JSONResult();
        try{
            clazzService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"班级删除","clazz:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            clazzService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("start")
    @ResponseBody
    public JSONResult start(Long id){
        JSONResult result = new JSONResult();
        try{
            clazzService.start(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
}
